package com.wxn.learncompose.pagers

import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import com.wxn.learncompose.MyApp
import com.wxn.learncompose.beans.ButtonState

@Composable
fun ButtonsPager() {

    Surface() {
        Column(
            modifier = Modifier
                .padding(all = 12.dp)
                .fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            //第一个按钮
            Button(onClick = {
                    Toast.makeText(MyApp.context(), "click!!!", Toast.LENGTH_SHORT).show()
                },
                modifier = Modifier
                    .size(width = 80.dp, height = 45.dp)
                    .clip(RoundedCornerShape(8.dp))
                    .background(Color.Gray),
                colors = ButtonDefaults.textButtonColors()
            ) {
                Text(text = "Sure",
                    modifier = Modifier.background(Color.Gray),
                    color = Color.White)
            }

            Spacer(modifier = Modifier.size(width = 1.dp, height = 12.dp))

            //第二个按钮
            Button(onClick = {
                Toast.makeText(MyApp.context(), "click!!", Toast.LENGTH_SHORT).show()
            },
                modifier = Modifier
                    .size(width = 150.dp, height = 45.dp)
                    .clip(RoundedCornerShape(8.dp))
                    .background(Color.DarkGray),
                colors = ButtonDefaults.textButtonColors()
                ) {
                Row(
                    modifier = Modifier.padding(all = 1.0.dp),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Center
                ) {
                    Icon(Icons.Filled.Favorite, contentDescription = "",
                        modifier = Modifier.size(ButtonDefaults.IconSize))
                    Spacer(modifier = Modifier.size(ButtonDefaults.IconSpacing))
                    Text(text = "favorite",
                        color = Color.White,
                        fontWeight = FontWeight.Bold,
                        fontSize = TextUnit(16f, TextUnitType.Sp)
                    )
                }
            }

            Spacer(modifier = Modifier.size(1.dp, 10.dp))

            //第三个按钮
            val interactionState = remember {
                MutableInteractionSource()
            }
            val (text, textColor, buttonColor) = when{
                //按下时设置的文字，文字颜色，按钮背景颜色
                interactionState.collectIsPressedAsState().value -> ButtonState("Pressed", Color.Red, Color.Gray)
                //正常显示时设置的文字，文字颜色，按钮背景颜色
                else -> ButtonState("Button", Color.Blue, Color.LightGray)
            }
            Button(onClick = {
                Toast.makeText(MyApp.context(), "CLICK!!!", Toast.LENGTH_SHORT).show()
            },
                interactionSource = interactionState, //状态监听
                modifier = Modifier.size(120.dp, 55.dp),
                shape = RoundedCornerShape(12.dp),
                colors = ButtonDefaults.buttonColors(
                    containerColor = buttonColor
                )) {
                Text(text = text,
                    color = textColor,
                    fontSize = TextUnit(16f, TextUnitType.Sp))
            }

        }
    }

}